package com.bosssoft.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bosssoft.entity.po.FibExpenseBudgetPO;
import com.bosssoft.entity.po.FibIncomeBudgetPO;
import com.bosssoft.entity.vo.FibIncomeBudgetVO;
import com.bosssoft.page.PageResult;
import com.bosssoft.response.Result;
import com.bosssoft.service.FibExpenseBudgetService;
import com.bosssoft.service.FibIncomeBudgetService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @author Li kexi
 * Created on 2021/8/17.16:12
 */
@RestController
@Api(tags = "部门用户-预算管理控制器")
@RequestMapping("/deptBudget")
public class DeptFibBudgetController {
    /**
     *
     * 预算控制数
     * 预算数据审阅（所有子部门预算的大类）
     * 预算审核通过（一上）
     * 预算审核打回
     * 报表查询
     * 预算综合查询
     */

    @Autowired
    private FibExpenseBudgetService fibExpenseBudgetService;

    @Autowired
    private FibIncomeBudgetService fibIncomeBudgetService;

    // 1 -> 初始
    // 2 -> 二级部门审核通过
    // 3 -> 不通过，让申报部门修改报表
    // 4 -> 一级部门审核通过
    // 5 -> 申报部门修改报表
    @PostMapping("/auditExpense")
    @ApiOperation("审核某单位的年度报表（支出）")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "deptId",name = "单位编码"),
            @ApiImplicitParam(value = "year",name = "年度"),
            @ApiImplicitParam(value = "nextStatus",name = "审核结果(本方法中只能是 2（通过） 或者 3（不通过）)"),
    })
    public Result auditExpense(Long deptId,Integer year,Integer nextStatus){

        // 1 -> 待审核状态
        Map<Long, List<FibExpenseBudgetPO>> map = fibExpenseBudgetService.getAllStatement(deptId, year, 1);

        List<FibExpenseBudgetPO> expenseBudgetPOS = map.get(deptId);
        for (FibExpenseBudgetPO expenseBudgetPO : expenseBudgetPOS){
            fibExpenseBudgetService.changeStatus(expenseBudgetPO,nextStatus);
        }
        // 审核不通过，要发操作意见
        return Result.ok();
    }

    @PostMapping("/auditIncome")
    @ApiOperation("审核某单位的年度报表(收入)")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "deptId",name = "单位编码"),
            @ApiImplicitParam(value = "year",name = "年度"),
            @ApiImplicitParam(value = "nextStatus",name = "审核结果(本方法中只能是 2（通过） 或者 3（不通过）)"),
    })
    public Result auditIncome(Long deptId,Integer year,Integer nextStatus){

        int b = fibIncomeBudgetService.changeStatus(deptId,year,nextStatus);
        return Result.ok();
    }

    @PostMapping("/queryMyIncomeAndChildren")
    @ApiOperation("查看当前部门的所有子部门的收入预算")
    public Result queryMyIncomeAndChildren(@RequestParam(defaultValue = "1")Integer pageNo,
                                           @RequestParam(defaultValue = "10")Integer pageSize,
                                           Long deptId){

        List<Page<FibIncomeBudgetPO>> pageList = fibIncomeBudgetService.queryMyIncomeAndChildren(pageNo,pageSize,deptId);
        return Result.ok().data("pageList",pageList);
    }

}
